* This is an opaque pointer. Callers must not fondle the contents of it.
*/
// This is a crutch until the new C++ shorthandle goes in.
-#define PRIME 37
-typedef struct {
- unsigned int target_len;
- char* badchars;
- char* goodchars;
- char* defname;
- queue namelist[PRIME];
-
- /* Various internal flags at end to allow alignment flexibility. */
- unsigned int mustupper:1;
- unsigned int whitespaceok:1;
- unsigned int repeating_whitespaceok:1;
- unsigned int must_uniq:1;
- unsigned int is_utf8:1;
-} mkshort_handle_imp;
+
+struct mkshort_handle_imp; // forward declare, definition in mkshort.cc
typedef mkshort_handle_imp* short_handle;
char* mkshort(short_handle, const char*);
*/
-#include "defs.h"
-#include "cet.h"
-#include "cet_util.h"
+#include <cctype> // for isspace, toupper, isdigit
+#include <cstdio> // for sprintf, size_t
+#include <cstring> // for strlen, memmove, strchr, strcpy, strncmp, strcat, strncpy
+
+#include <QtCore/QList> // for QList
+#include <QtCore/QString> // for QString
+#include <QtCore/QtGlobal> // for foreach
-#include <cctype>
-#include <cstdio>
-#include <cstdlib>
-#include <cstring>
+#include "defs.h"
+#include "cet.h" // for cet_utf8_strdup, cet_utf8_strlen, cet_utf8_strndup
+#include "cet_util.h" // for cet_cs_vec_utf8
#define MYNAME "mkshort"
static const char vowels[] = "aeiouAEIOU";
-#define DEFAULT_TARGET_LEN 8
+static constexpr unsigned int default_target_len = 8U;
static const char* DEFAULT_BADCHARS = "\"$.,'!-";
/*
* string hash mixes things up enough that strcmp can generally bail on the
* first byte or two for a mismatch.
*/
-#define PRIME 37
+static constexpr unsigned int prime = 37U;
+
+struct uniq_shortname {
+ char* orig_shortname{nullptr};
+ int conflictctr{0};
+};
-typedef struct {
- queue list;
- char* orig_shortname;
- int conflictctr;
-} uniq_shortname;
+struct mkshort_handle_imp {
+ unsigned int target_len{default_target_len};
+ char* badchars{nullptr};
+ char* goodchars{nullptr};
+ char* defname{nullptr};
+ QList<uniq_shortname*> namelist[prime];
+
+ /* Various internal flags */
+ bool mustupper{false};
+ bool whitespaceok{true};
+ bool repeating_whitespaceok{false};
+ bool must_uniq{true};
+ bool is_utf8{false};
+};
static struct replacements {
const char* orig;
while (*key) {
hash = ((hash<<5) ^ (hash>>27)) ^ toupper(*key++);
}
- hash = hash % PRIME;
+ hash = hash % prime;
return hash;
}
short_handle
mkshort_new_handle()
{
- mkshort_handle_imp* h = (mkshort_handle_imp*) xcalloc(sizeof *h, 1);
-
- for (auto &i : h->namelist) {
- QUEUE_INIT(&i);
- }
+ auto h = new mkshort_handle_imp;
- h->whitespaceok = 1;
h->badchars = xstrdup(DEFAULT_BADCHARS);
- h->target_len = DEFAULT_TARGET_LEN;
- h->must_uniq = 1;
h->defname = xstrdup("WPT");
h->is_utf8 = (global_opts.charset == &cet_cs_vec_utf8);
uniq_shortname*
is_unique(mkshort_handle_imp* h, char* name)
{
- queue* e, *t;
int hash = hash_string(name);
- QUEUE_FOR_EACH(&h->namelist[hash], e, t) {
- uniq_shortname* z = reinterpret_cast<uniq_shortname *>(e);
+ foreach (uniq_shortname* z, h->namelist[hash]) {
if (0 == case_ignore_strcmp(z->orig_shortname, name)) {
return z;
}
uniq_shortname* s = (uniq_shortname*) xcalloc(1, sizeof(uniq_shortname));
s->orig_shortname = xstrdup(name);
- ENQUEUE_TAIL(&h->namelist[hash], &s->list);
+ h->namelist[hash].append(s);
}
char*
}
for (auto &i : hdr->namelist) {
- queue* e, *t;
- QUEUE_FOR_EACH(&i, e, t) {
- uniq_shortname* s = reinterpret_cast<uniq_shortname *>(e);
+ while (!i.isEmpty()) {
#if 0
if (global_opts.verbose_status >= 2 && s->conflictctr) {
fprintf(stderr, "%d Output name conflicts: '%s'\n",
s->conflictctr, s->orig_shortname);
}
#endif
- dequeue(e);
+ auto s = i.takeFirst();
xfree(s->orig_shortname);
xfree(s);
}
xfree(hdr->defname);
}
- xfree(hdr);
+ delete hdr;
*h = nullptr;
}
{
mkshort_handle_imp* hdl = (mkshort_handle_imp*) h;
if (l == 0) {
- hdl->target_len = DEFAULT_TARGET_LEN;
+ hdl->target_len = default_target_len;
} else {
hdl->target_len = l;
}